﻿//弹出选择窗口
function showSmartPickerPage(sender, url, dlgWidth, dlgHeight) {
    //初始化窗口高度、宽度
    var dlgLeft = (window.screen.width - dlgWidth) / 2;
    var dlgTop = (window.screen.height - dlgHeight) / 2;
    var widthTmp = dlgWidth;
    var setting = "scroll:no;status:no;dialogHeight:" + dlgHeight + "px;dialogWidth:" + widthTmp + "px;dialogLeft:" + dlgLeft + ";dialogTop:" + dlgTop;

    //获取当前已选择的项
    var selectedItems = [];
    var idString = $(sender).parent().find("input[id*='SelectedID']").val();
    var textString = $(sender).parent().find("input[id*='SelectedText']").val();
    if (idString.length > 0) {
        var idArr = [], textArr = [];
        idArr = idString.split(";");
        textArr = textString.split(";");
        for (var i = 0; i < idArr.length; i++) {
            selectedItems.push({ id: idArr[i].toString(), text: textArr.length > i ? textArr[i] : '' });
        }
    }

    //选择完毕返回选择的项
    var selectedItems = window.showModalDialog(url, selectedItems, setting);
    if (selectedItems) {
        var ids = [], texts = [];
        $.each(selectedItems, function (index, item) {
            ids.push(item.id);
            texts.push(item.text);
        });
        $(sender).parent().find("input[id*='SelectedID']").val(ids.join(";"));
        $(sender).parent().find("input[id*='SelectedText']").val(texts.join(";"));

        //若设置了回调函数，则执行回调函数
        if ($.isFunction(arguments[4])) {
            arguments[4](sender, selectedItems);
        }
    }
}

//加载后将文本框设置为只读
$(function () {
    $("input[id*='SelectedText']").attr("readonly", true);
});